using System;
using NHibernate;

namespace ClubFit.DataAccess
{
    public class MealPersistence : IMealPersistence
    {
        private readonly IConfigure configure;
        private ITransaction transaction;

        public MealPersistence(IConfigure configure)
        {
            this.configure = configure;
        }

        #region IMealPersistence Members

        public object[] GetAll()
        {
            throw new NotImplementedException();
        }

        public void Save(object[] items)
        {
            using(ISession session = configure.CreateSession())
            {
                try
                {
                    session.Transaction.Begin();
                    session.Save(items);
                    transaction.Commit();
                    session.Close();
                }
                catch(Exception ex)
                {
                    session.Transaction.Rollback();
                }
            }
        }

        public void Update(object item)
        {
            throw new NotImplementedException();
        }

        public void Delete(object item)
        {
            using (ISession session = configure.CreateSession())
            {
                try
                {
                    session.Transaction.Begin();
                    session.Delete(item);
                    transaction.Commit();
                    session.Close();
                }
                catch (Exception ex)
                {
                    session.Transaction.Rollback();
                }
            }
        }

        public object Get(int id)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}